package Waya::App::FileIterator;

use strict;
use warnings;
use Waya::SGF;


our $VERSION = '0.1';


use base 'Waya::App';


__PACKAGE__
    ->mk_boolean_accessors(qw(should_write))
    ->mk_scalar_accessors(qw(original_filename filename game));


use constant GETOPT => (qw/nobak/);


sub app_code {
    my $self = shift;
    for my $file (@ARGV) {
        $self->original_filename($file);
        $self->filename($file);
        $self->set_should_write;
        $self->log->info('processing %s', $file);
        $self->game(Waya::SGF->new_from_file($file));
        $self->process_game;

        if (defined $self->game && $self->should_write) {
            $self->game->write_file(
                $self->original_filename, $self->filename,
                nobak => $self->opt->{nobak}
            );
        }
    }
}


sub process_game {}


# treat an option as a command to set the game info

sub apply_game_info_option {
    my ($self, @option) = @_;
    my $info = $self->game->game_info_node;
    for my $key (@option) {
        my $value = $self->opt->{$key};
        next unless defined $value;

        if ($value eq 'undef') {
            delete $info->{$key};
        } else {
            $info->{$key} = $value;
        }
    }
}


1;
